package com.yun.baymax.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.yun.baymax.R;
import com.yun.baymax.adapter.TrendsAdapter;
import com.yun.baymax.bean.User;
import com.yun.baymax.bean.adapter.ProfileType;
import com.yun.baymax.bean.adapter.Trends;
import com.yun.baymax.cache.UserCache;
import com.yun.lib.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class ProfileFragment extends BaseFragment {
    private static Context mContext;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    List<MultiItemEntity> list = new ArrayList<>();
    private User bmUser;
    private User localUser;
    private TrendsAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_profile;
    }

    @Override
    protected void initViews(Bundle savedInstanceState, View view, LayoutInflater inflater) {
        ButterKnife.bind(this, view);
        localUser = UserCache.getInstance().getCurrentUser();
        bmUser = BmobUser.getCurrentUser(User.class);
    }

    @Override
    protected void loadData() {
        ProfileType profileType1 = new ProfileType(R.drawable.ic_my_favor, "我的收藏");
        list.clear();
        list.add(profileType1);
        final ProfileType profileType = new ProfileType(R.drawable.ic_idea, "最新动态");

        BmobQuery<Trends> query = new BmobQuery<Trends>();
        query.addWhereEqualTo("author", new BmobPointer(bmUser));
        query.setLimit(20);
        query.order("-createdAt");
        query.include("author");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(new FindListener<Trends>() {
            @Override
            public void done(List<Trends> object, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < object.size(); i++) {
                        profileType.addSubItem(object.get(i));
                    }
                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
        list.add(profileType);

        initViewData();
    }

    private void initViewData() {
        if (bmUser == null)
            return;
        localUser = localUser == null ? bmUser : localUser;


        adapter = new TrendsAdapter(list);

        final LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRecyclerView.setAdapter(adapter);
        // important! setLayoutManager should be called after setAdapter
        mRecyclerView.setLayoutManager(manager);
//        adapter.expandAll();
    }

    private void updateUserInfo(User user) {
        UserCache.getInstance().updateUser(localUser);
        localUser.setEmail(null);
        localUser.update(user.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    showToast("更新成功");
                    initViewData();
                } else {
                    showToast("更新失败" + e.getMessage());
                }
            }
        });
    }

    @OnClick({})
    public void onClick(View view) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

    }


    private void showDialog(String title, final String value, final OnValueChangeListener listener) {

        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

        final EditText et = new EditText(mContext);
        et.setText(TextUtils.isEmpty(value) ? "" : value);
        et.setSelection(TextUtils.isEmpty(value) ? 0 : value.length());
        dialog.setTitle(title);
        dialog.setView(et);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String s = et.getText().toString();
                if (!TextUtils.equals(s, value) && listener != null) {
                    listener.onChangedValue(s);
                }
                dialogInterface.dismiss();
            }
        });

        dialog.show();

    }

    interface OnValueChangeListener {
        void onChangedValue(String value);
    }
}
